﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace TetrisLibrary {
    public partial class TetrisBlockPreviewView : UserControl {


        public static int BlockSize = 30;
        private Pen pen = new Pen(Color.White);
        private int[] tetrisShape;

        public int[] TetrisShape {
            get => tetrisShape;
            set {
                if (value == null || value.Length <= 0) return;
                tetrisShape = value;
                Invalidate();
            }
        }

        public TetrisBlockPreviewView() {
            InitializeComponent();
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            SetStyle(ControlStyles.AllPaintingInWmPaint, true);
        }

        private void TetrisBlockPreviewView_Paint(object sender, PaintEventArgs e) {
            Width = 6 + 1 + BlockSize * 6;
            Height = Width;

            //绘制游戏背景
            e.Graphics.Clear(Color.White);
            pen.Color = Color.Gray;
            for (int y = 0; y < 6; y++) {
                for (int x = 0; x < 6; x++) {
                    e.Graphics.FillRectangle(new SolidBrush(Color.DarkGray), x + 1 + BlockSize * x,
                        y + 1 + BlockSize * y, BlockSize, BlockSize);
                }
            }

            if (tetrisShape != null && tetrisShape.Length > 0) {
                //绘制当前的方块状态
                for (int i = 0; i < 16; i++) {
                    if (tetrisShape[i] == 1) {
                        int dx = i % 4 + 1;
                        int dy = i / 4 + 1;
                        e.Graphics.FillRectangle(new SolidBrush(Color.Red), dx + 1 + dx * BlockSize,
                            dy + 1 + dy * BlockSize, BlockSize, BlockSize);
                    }
                }
            }
        }
    }
}

